package com.jianglin.controller;

import com.github.pagehelper.PageInfo;
import com.jianglin.model.*;
import com.jianglin.service.IHouseService;
import com.jianglin.service.IOwnerService;
import com.jianglin.service.IPropertyInfoService;
import com.jianglin.service.IPropertyTypeService;
import com.jianglin.util.JsonObject;
import com.jianglin.util.JwtUtil;
import com.jianglin.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
/**
 *
 *
 * @author liyijia
 */
@Api(tags = {"物业费管理"})
@SuppressWarnings("SpellCheckingInspection")
@CrossOrigin(origins="http://localhost:5500")
@RestController
@RequestMapping("/propertyinfo")
public class PropertyInfoController {

    @Resource
    private IPropertyInfoService propertyInfoService;

    @Resource
    private IHouseService houseService;

    @Resource
    private IOwnerService ownerService;

    @Resource
    private IPropertyTypeService propertyTypeService;

    @Resource
    private JwtUtil jwtUtil;

    @RequestMapping("/queryPropertyAll")
    public JsonObject<PropertyInfo> queryPropertyAll(PropertyInfo propertyInfo, String numbers,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "15") Integer limit){
        if(numbers!=null){
            House house=new House();
            house.setNumbers(numbers);
            propertyInfo.setHouse(house);
        }
       //返回列表的形式
        PageInfo<PropertyInfo> pageInfo=propertyInfoService.findPropertyInfoAll(page,limit,propertyInfo);
        return new JsonObject<>(0,"ok",pageInfo.getTotal(),pageInfo.getList());

    }


    @RequestMapping("/queryPropertyAll2")
    public JsonObject<PropertyInfo> queryPropertyAll2(PropertyInfo propertyInfo, HttpServletRequest request,
                                       @RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "15") Integer limit){


      //先获取业主的token令牌
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7).trim();
        //根据令牌获取业主username
        String username = jwtUtil.getUsernameFromToken(token);

        Owner owner=ownerService.queryOwnerByName(username);
        //业主关联的房屋
        Integer houId= owner.getHouseId();
        propertyInfo.setHouseId(houId);
        PageInfo<PropertyInfo> pageInfo=propertyInfoService.findPropertyInfoAll(page,limit,
                propertyInfo);
        return new JsonObject<>(0,"ok",pageInfo.getTotal(),pageInfo.getList());

    }


    @PostMapping("/initData")
    public R initData(@RequestBody PropertyInfo propertyInfo) {
        List<House> list = houseService.findList();

        for (House house : list) {
            // 查询物业费收费标准
            //增加的就是物业费，id为1，转换为long
            PropertyType type = propertyTypeService.findById(1L);
            if (type == null) {continue;}

            double price = type.getPrice();
            Integer status = house.getStatus();

            // 检查房屋状态是否有效
            if (status != null && status != 0) {
                // 计算物业费
                double money = house.getArea() * price;

                // 创建新的物业信息对象
                PropertyInfo newInfo = new PropertyInfo();
                newInfo.setMoney(money);
                newInfo.setHouseId(house.getId());
                //默认未缴费
                newInfo.setStatus(0);
                newInfo.setTypeId(1);
                newInfo.setStartDate(propertyInfo.getStartDate());
                newInfo.setEndDate(propertyInfo.getEndDate());
                newInfo.setRemarks(propertyInfo.getRemarks());

                // 保存物业信息
                propertyInfoService.add(newInfo);
            }
        }
        return R.ok();
    }

    @ApiOperation(value = "删除")
    @RequestMapping("/deleteByIds")
    public R delete(String ids){
        String[] list= ids.split(",");
        for(String id:list){
            Long idLong=new Long(id);
            propertyInfoService.delete(idLong);
        }
        return R.ok();
    }


    @ApiOperation(value = "更新")
    @RequestMapping("/update")
    public R update(Integer id) {
        // 只更新状态字段
        int num = propertyInfoService.updateStatus(id.longValue(), 1);
        return num > 0 ? R.ok() : R.fail("更新失败");
    }

    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码"),
        @ApiImplicitParam(name = "pageCount", value = "每页条数")
    })
    @GetMapping
    public PageInfo<PropertyInfo> findListByPage(@RequestParam Integer page,
                                                 @RequestParam Integer pageCount) {
        return propertyInfoService.findListByPage(page, pageCount);
    }

    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public PropertyInfo findById(@PathVariable Long id){
        return propertyInfoService.findById(id);
    }

}
